<!-- 添加 -->
<div class="layui-fluid content_box">
	<div  class="main_content_wrap">
		<form lay-filter="modifyForm" class="layui-form model-form">
			<input name="id" hidden/>
			<div class="layui-form-item">
				<label class="layui-form-label">成果名称</label>
				<div class="layui-input-block">
					<input name="name"  type="text" autocomplete="off" class="layui-input" required/>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">发明人</label>
				<div class="layui-input-inline" style="width: 500px;">
					<input name="inventor"  type="text" autocomplete="off" class="layui-input" required/>
				</div>
				<div class="layui-form-mid layui-word-aux">（多个发明人用顿号分割）</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">成果类型</label>
				<div class="layui-input-inline">
					<select name="type" id="knowledge_type" required>
					</select>
				</div>

				<label class="layui-form-label">所有权人</label>
				<div class="layui-input-inline">
					<select name="ownership" id="knowledge_ownership" required>
					</select>
				</div>

				<label class="layui-form-label">知识产权号</label>
				<div class="layui-input-inline">
					<input name="knowledge_num"  type="text" autocomplete="off" required class="layui-input"/>
				</div>

				<label class="layui-form-label" style="width: 120px">知识产权有效日期</label>
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" name="knowledge_start_time" placeholder="开始时间" class="layui-input" id='starDate' required autocomplete="off">
				</div>
				<div class="layui-form-mid"> - </div>
				<div class="layui-input-inline" style="width: 100px;">
					<input type="text" name="knowledge_end_time" placeholder="结束时间" class="layui-input" id='endDate' required autocomplete="off">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">转化方式</label>
				<div class="layui-input-inline">
					<select name="conversion_type" required id="conversion_type">
					</select>
				</div>

				<label class="layui-form-label">收益方式</label>
				<div class="layui-input-inline">
					<select name="conversion_get_money_type" required id="conversion_get_money_type">
					</select>
				</div>

				<label class="layui-form-label">转化预收益</label>
				<div class="layui-input-inline">
					<input name="conversion_get_money"  type="number" placeholder="请输入预收益" class="layui-input" required lay-verify="required|number|positiveNumber" autocomplete="off"/>
				</div>
				<div class="layui-form-mid layui-word-aux">元</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">计划进度</label>
				<div class="layui-input-block">
					<textarea name="progress_plan" placeholder="请输入计划进度" class="layui-textarea"></textarea>
				</div>
			</div>
			<hr>
			<div class="main_content_wrap">
				<p class="add_title">受让单位及法定代表人</p>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">单位全称</label>
				<div class="layui-input-block">
					<input name="to_company_name"  type="text" autocomplete="off" required class="layui-input"/>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">注册地址</label>
				<div class="layui-input-block">
					<input name="to_company_registerded_addresss"  type="text" autocomplete="off" required class="layui-input"/>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">通讯地址</label>
				<div class="layui-input-block">
					<input name="to_company_addresss"  type="text" autocomplete="off" required class="layui-input"/>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">法人姓名</label>
				<div class="layui-input-inline">
					<input name="to_company_legal_person"  type="text" autocomplete="off" required class="layui-input"/>
				</div>

				<label class="layui-form-label">联系电话</label>
				<div class="layui-input-inline">
					<input name="to_company_legal_phone"  type="text" autocomplete="off" required lay-verify="required|phoneNum" class="layui-input"/>
				</div>

				<label class="layui-form-label" style="width: 200px">法人身份证号/统一社会信用代码</label>
				<div class="layui-input-inline">
					<input name="to_company_union_no"  type="text" autocomplete="off" required lay-verify="required|socialCreditCode" class="layui-input"/>
				</div>
			</div>

			<hr>
			<div class="main_content_wrap">
				<p class="add_title">主要完成人</p>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">姓名</label>
				<div class="layui-input-inline">
					<select id="main_finished_person_name" name="main_finished_person_name" required lay-filter="user_name">
					</select>
				</div>

				<label class="layui-form-label">联系电话</label>
				<div class="layui-input-inline">
					<input name="main_finished_person_phone" id="main_finished_person_phone" required lay-verify="required|phoneNum" type="text" autocomplete="off" class="layui-input"/>
				</div>

				<label class="layui-form-label">身份证号</label>
				<div class="layui-input-inline">
					<input name="main_finished_person_cardno"  type="text" autocomplete="off" lay-verify="required|idcard" required class="layui-input"/>
				</div>
			</div>
			<div class="main_content_wrap">
				<p class="add_title">其他信息</p>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">所在二级单位</label>
				<div class="layui-input-block">
					<select id="second_company" name="second_company">
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">盖章附件pdf</label>
				<button type="button" class="layui-btn" id="upload_file">
					<i class="layui-icon">&#xe67c;</i>选择文件
				</button>
				<div class="layui-inline" id="file_name" style="display: none">
				</div>
				<input hidden name="req_file_pdf" id="file_url"/>
			</div>
			<div class="layui-form-item text-center">
				<button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
				<button type="button" lay-filter="modifyFormSubmit" lay-submit class="layui-btn site-demo-active">确认</button>
			</div>
		</form>
	</div>
</div>
<script>

	layui.use(['jquery','form','config','upload','layer','laydate'], function(){
		var $ = layui.jquery;
		var form = layui.form;
		var config = layui.config;
		var layer = layui.layer;
		var laydate = layui.laydate;
		var upload = layui.upload;
		var router = layui.router();
		var id = router.search.id; //获取id

		var startDate = laydate.render({
			elem: '#starDate', //指定元素
			format: 'yyyy-MM-dd', //可任意组合
			done: function(value, date){
				endDate.config.min ={
					year:date.year,
					month:date.month-1, //关键
					date: date.date,
				};
			}
		});
		var endDate =laydate.render({
			elem: '#endDate', //指定元素
			format: 'yyyy-MM-dd', //可任意组合
			done: function(value,date) {
				startDate.config.max = {
					year: date.year,
					month: date.month - 1,//关键
					date: date.date,
				}
			}

		});
		console.log('222222222222222222222222');
		if(!knowledgeTypeList){
			$.get(serverUrlAdmin + 'Common/getListByType.html', {}, function (res) {
				if (res.success) {
					knowledgeTypeList=res.obj.knowledge_type;
					ownershipList=res.obj.ownership;
					conversionTypeList=res.obj.conversion_type;
					moneyTypeList=res.obj.money_type;
					pricingStrategyList=res.obj.pricing_strategy;
					stateList=res.obj.state_list;
					setSel();
				}
			}, 'json');
		}else if(!userList) {
			layer.load(2);
			$.get(serverUrlAdmin + 'Common/common_lst.html', {}, function (res) {
				layer.closeAll('loading');
				if (res.success) {
					userList=res.obj.user_list;
					departmentList=res.obj.department_list;
					setSel();
				}
			}, 'json');
		}else {
			setSel();
		}

		function setSel() {
			var knowledge_type_str='<option value="">请选择</option>';
			for(var i=0;i<knowledgeTypeList.length;i++){
				knowledge_type_str+='<option value="'+knowledgeTypeList[i]+'">'+knowledgeTypeList[i]+'</option>';
			}
			$('#knowledge_type').html(knowledge_type_str);

			var knowledge_ownership_str='<option value="">请选择</option>';
			for(var i=0;i<ownershipList.length;i++){
				knowledge_ownership_str+='<option value="'+ownershipList[i]+'">'+ownershipList[i]+'</option>';
			}
			$('#knowledge_ownership').html(knowledge_ownership_str);

			var conversion_type_str='<option value="">请选择</option>';
			for(var i=0;i<conversionTypeList.length;i++){
				conversion_type_str+='<option value="'+conversionTypeList[i]+'">'+conversionTypeList[i]+'</option>';
			}
			$('#conversion_type').html(conversion_type_str);

			var conversion_get_money_type_str='<option value="">请选择</option>';
			for(var i=0;i<moneyTypeList.length;i++){
				conversion_get_money_type_str+='<option value="'+moneyTypeList[i]+'">'+moneyTypeList[i]+'</option>';
			}
			$('#conversion_get_money_type').html(conversion_get_money_type_str);

			var main_finished_person_name_str='<option value="">请选择</option>';
			for(var i=0;i<userList.length;i++){
				main_finished_person_name_str+='<option value="'+userList[i].nick_name+'">'+userList[i].nick_name+'</option>';
			}
			$('#main_finished_person_name').html(main_finished_person_name_str);

			var second_company_str='<option value="">请选择</option>';
			for(var i=0;i<departmentList.length;i++){
				second_company_str+='<option value="'+departmentList[i]+'">'+departmentList[i]+'</option>';
			}
			$('#second_company').html(second_company_str);

			form.render();
			getInfo();
		}
		form.render();

		function getInfo(){
			if(id){
				$.ajax({
					type:'get',
					url:serverUrlAdmin+'Knowledge/getDetail',
					data:{'id': id},
					dataType:"json",
					// async:false,
					success:function(res){
						if(res.success===true){
							if(res.obj.req_file_pdf){
								var url = res.obj.req_file_pdf;
								var fileName = url.substring(url.lastIndexOf("/") + 1);
								$('#file_name').show();
								$('#file_name').html(fileName)
							}
							form.val('modifyForm', res.obj);
							form.render();
						}
						return false;
					},
					error:function(error){
						console.log(error);
					}
				});
			}
		}
		form.on('select(user_name)', function(data){
			var nick_name = data.value;
			for(var i=0;i<userList.length;i++){
				if(nick_name === userList[i].nick_name){
					$('#main_finished_person_phone').val(userList[i].phone_no);
				}
			}
		});

		var uploadInst = upload.render({
			elem: '#upload_file' //绑定元素
			,url: serverUrlAdmin + 'File/uploadFile.html' //上传接口
			,accept:'file'
			,data:{
				'dicName':'sxdxprojectfile',
				'type':'pdf'
			}
			,done: function(res){
				//上传完毕回调
				if(res.success === true){
					layer.msg('上传成功', {icon: 1,time:300}, function () {
						$('#req_file_pdf').val(res.obj.file);
						$('#file_name').show();
						$('#file_name').html(res.obj.file_name)
						form.render(null,'modifyForm');
					});
				}else {
					layer.closeAll('page');
					layer.msg(res.msg, {icon: 2});
				}
			}
			,error: function(){
				//请求异常回调
			}
		});


		//监听提交
		form.on('submit(modifyFormSubmit)', function(data){
			var DISABLED = 'layui-btn-disabled';
			// 增加样式
			$('.site-demo-active').addClass(DISABLED);
			// 增加属性
			$('.site-demo-active').attr('disabled', 'disabled');
			$.post(serverUrlAdmin+'Knowledge/modifyInfo.html', data.field, function (res) {
				if (res.success === true) {
					layer.msg('提交成功', {icon: 1, time:300}, function () {
						location.replace('#/knowledge/index');
					});
				}else {
					layer.msg(res.msg, {icon: 2});
					$('.site-demo-active').removeClass(DISABLED);
					$('.site-demo-active').removeAttr('disabled');
				}
			}, 'json');
			return false;
		});

		form.verify({
			numberDh: function(value, item){
				var reg = /^[0-9]+(,[0-9]+)*$/; // 正则表达式，匹配数字和逗号
				if(!reg.test(value)){
					return '请输入正确的金额';
				}
			},
			socialCreditCode:function (Code){
				let reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
				if (!reg.test(Code)) {
					var patrn = /^[0-9A-Z]+$/;
					//18位校验及大写校验
					if ((Code.length != 18) || (patrn.test(Code) == false)) {
						return '不是有效的身份证号或统一社会信用编码';
					}else {
						var Ancode;//统一社会信用代码的每一个值
						var Ancodevalue;//统一社会信用代码每一个值的权重
						var total = 0;
						var weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];//加权因子
						var str = '0123456789ABCDEFGHJKLMNPQRTUWXY';
						//不用I、O、S、V、Z
						for (var i = 0; i < Code.length - 1; i++) {
							Ancode = Code.substring(i, i + 1);
							Ancodevalue = str.indexOf(Ancode);
							total = total + Ancodevalue * weightedfactors[i];
							//权重与加权因子相乘之和
						}
						var logiccheckcode = 31 - total % 31;
						if (logiccheckcode == 31) {
							logiccheckcode = 0;
						}
						var Str = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y";
						var Array_Str = Str.split(',');
						logiccheckcode = Array_Str[logiccheckcode];

						var checkcode = Code.substring(17, 18);
						if (logiccheckcode != checkcode) {
							return '不是有效的身份证号或统一社会信用编码';
						}
					}
				}
			},
			positiveNumber:[/^(([0-9]+[\.]?[0-9]+)|[1-9])$/,'请输入正确的金额'],
			email:[/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/,'请填写正确的邮箱号'],
			phoneNum:[/^1\d{10}$/,'请填写正确的手机号'],
			idcard:[/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,'请填写正确的身份证号码'],
		});
	});
</script>